package com.ram.service.impl;

import com.ram.mapper.AccommodationMapper;
import com.ram.mapper.BedMapper;
import com.ram.mapper.RoomMapper;
import com.ram.pojo.Accommodation;
import com.ram.pojo.AccommodationDTO;
import com.ram.pojo.AccommodationVO;
import com.ram.pojo.PageResult;
import com.ram.service.AccommodationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class AccommodationServiceImpl implements AccommodationService {
    @Autowired
    private AccommodationMapper accommodationMapper;
    @Autowired
    private BedMapper bedMapper;
    @Autowired
    private RoomMapper roomMapper;

    @Override
    public List<Accommodation> list() {
        return accommodationMapper.list();
    }

    @Override
    public Accommodation getById(Integer id) {
        return accommodationMapper.getById(id);
    }

    @Override
    public void add(Accommodation accommodation) {
        accommodation.setCreateTime(LocalDateTime.now());
        accommodation.setUpdateTime(LocalDateTime.now());
        accommodationMapper.insert(accommodation);
    }

    @Override
    public void update(Accommodation accommodation) {
        accommodation.setUpdateTime(LocalDateTime.now());
        accommodationMapper.update(accommodation);
    }

    @Override
    public void deleteById(Integer id) {
        accommodationMapper.deleteById(id);
    }

    @Override
    public PageResult<AccommodationVO> pageQuery(AccommodationDTO dto) {
        List<AccommodationVO> list = accommodationMapper.pageQuery(dto);
        long total = accommodationMapper.countPageQuery(dto);
        return new PageResult<>(total, list);
    }

    // ================== 业务流程 ==================
    @Override
    @Transactional
    public void checkin(Accommodation accommodation) {
        // 1. 新增住宿登记
        accommodation.setStatus(1); // 在住
        accommodation.setCreateTime(LocalDateTime.now());
        accommodation.setUpdateTime(LocalDateTime.now());
        accommodationMapper.insert(accommodation);
        // 2. 床位状态设为占用(2)
        bedMapper.updateStatus(accommodation.getBedId(), 2);
        // 3. 判断房间是否已满
        Integer roomId = bedMapper.getRoomIdByBedId(accommodation.getBedId());
        int totalBeds = bedMapper.countByRoomId(roomId);
        int usedBeds = bedMapper.countUsedByRoomId(roomId);
        if (totalBeds == usedBeds) {
            roomMapper.updateStatus(roomId, 2); // 2已满
        } else {
            roomMapper.updateStatus(roomId, 1); // 1空闲
        }
    }

    @Override
    @Transactional
    public void checkout(Integer accommodationId) {
        // 1. 更新住宿登记状态为已退(2)，并记录退宿时间
        Accommodation acc = accommodationMapper.getById(accommodationId);
        acc.setStatus(2);
        acc.setCheckoutDate(LocalDateTime.now().toLocalDate());
        acc.setUpdateTime(LocalDateTime.now());
        accommodationMapper.update(acc);
        // 2. 床位状态设为空闲(1)
        bedMapper.updateStatus(acc.getBedId(), 1);
        // 3. 判断房间是否有空闲床位
        Integer roomId = bedMapper.getRoomIdByBedId(acc.getBedId());
        int totalBeds = bedMapper.countByRoomId(roomId);
        int usedBeds = bedMapper.countUsedByRoomId(roomId);
        if (usedBeds == 0) {
            roomMapper.updateStatus(roomId, 1); // 1空闲
        } else {
            roomMapper.updateStatus(roomId, 2); // 2已满
        }
    }

    @Override
    @Transactional
    public void changeBed(Integer accommodationId, Integer newBedId, String remark) {
        // 1. 获取原住宿登记
        Accommodation acc = accommodationMapper.getById(accommodationId);
        Integer oldBedId = acc.getBedId();
        // 2. 退原床位
        bedMapper.updateStatus(oldBedId, 1);
        // 3. 占用新床位
        bedMapper.updateStatus(newBedId, 2);
        // 4. 更新住宿登记
        acc.setBedId(newBedId);
        acc.setRemark((acc.getRemark() == null ? "" : acc.getRemark()) + ";调宿:" + remark);
        acc.setUpdateTime(LocalDateTime.now());
        accommodationMapper.update(acc);
        // 5. 联动房间状态
        Integer oldRoomId = bedMapper.getRoomIdByBedId(oldBedId);
        Integer newRoomId = bedMapper.getRoomIdByBedId(newBedId);
        int oldUsed = bedMapper.countUsedByRoomId(oldRoomId);
        int oldTotal = bedMapper.countByRoomId(oldRoomId);
        int newUsed = bedMapper.countUsedByRoomId(newRoomId);
        int newTotal = bedMapper.countByRoomId(newRoomId);
        roomMapper.updateStatus(oldRoomId, oldUsed == 0 ? 1 : 2);
        roomMapper.updateStatus(newRoomId, newUsed == newTotal ? 2 : 1);
    }
} 